/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 */

package src.clovece.game;

import java.awt.event.MouseListener;
import java.util.Random;

/**
 *  Class representing computer player
 */
public class AiPlayer extends Player implements MouseListener {
    private boolean randomMove = true;
    private boolean preferHome = false;
    private boolean preferNew = false;
    
    public AiPlayer(int gameSet, int figurine, int number) {
        super(gameSet,figurine,number,"Computer " + number);
    }

    public void throwDie() {
        System.out.println("AiPlayer:throwDie");
        this.gameManagement.getInfoPanel().throwDie();
    }

    public void performMove() {
        /*
         *  RANDOM MOVE
         */
        if(this.randomMove) {
            this.randomMove();
        }
        /*
         *  LONGEST JUMP
         */
        else {
            this.longestJump();
        }
    }

    private void randomMove() {
        this.gameInfo.setSelectedFigurine(null);
        Random generator = new Random();
        int chosenMove = 0;
        int decide = generator.nextInt(101);
        //  PREFER
        //  HOME
        if(this.preferHome) {
            if(this.preferNew && decide > 40) {
                decide = generator.nextInt(this.gameInfo.getPosibleMovesFigurines().size());
                for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                    if(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition() == -1) {
                        this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                        chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                        break;
                    }
                }
                if(this.gameInfo.getSelectedFigurine() == null) {
                    this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(decide).getNumber()]);
                    chosenMove = this.gameInfo.getPosibleMovesFields().get(decide).getPosition();
                }
            }
            else {
                decide = generator.nextInt(this.gameInfo.getPosibleMovesFigurines().size());
                for(int i = 0; i < this.gameInfo.getPosibleMovesFields().size(); i++) {
                    if(this.gameInfo.getPosibleMovesFields().get(i).getPosition() >= this.gameInfo.getGameBoardSize()) {
                        this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                        chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                        break;
                    }
                }
                if(this.gameInfo.getSelectedFigurine() == null) {
                    this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(decide).getNumber()]);
                    chosenMove = this.gameInfo.getPosibleMovesFields().get(decide).getPosition();
                }
            }
        }
        //  PREFER
        //  NEW
        else if(this.preferNew) {
            decide = generator.nextInt(this.gameInfo.getPosibleMovesFigurines().size());
            for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                if(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition() == -1) {
                    this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                    chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                    break;
                }
            }
            if(this.gameInfo.getSelectedFigurine() == null) {
                this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(decide).getNumber()]);
                chosenMove = this.gameInfo.getPosibleMovesFields().get(decide).getPosition();
            }
        }
        //  PREFER
        //  NOTHING
        else {
            decide = generator.nextInt(this.gameInfo.getPosibleMovesFigurines().size());
            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(decide).getNumber()]);
            chosenMove = this.gameInfo.getPosibleMovesFields().get(decide).getPosition();
            System.out.println("Decided on move: " + decide + " to " + chosenMove);
        }
        System.out.print("Figurine n.");
        System.out.print(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(decide).getNumber()].getNumber());
        System.out.print(" on position:");
        System.out.print(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(decide).getNumber()].getActualPosition());
        System.out.println(" will move to field: " + this.gameInfo.getPosibleMovesFields().get(decide).getPosition());
        System.out.println("CPU has performed it's move.");
        this.gameInfo.setPosibleMovesFields(null);
        this.gameInfo.setPosibleMovesFigurines(null);
        this.gameManagement.moveFigurine(this.gameInfo.getFields()[chosenMove]);
    }

    private void longestJump() {
        this.gameInfo.setSelectedFigurine(null);
        Random generator = new Random();
        int chosenMove = 0;
        int decide = generator.nextInt(101);
        //  PREFER
        //  HOME
        if(this.preferHome) {
            // NEW
            boolean found = false;
            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(0).getNumber()]);
            chosenMove = this.gameInfo.getPosibleMovesFields().get(0).getPosition();
            if(this.preferNew && decide > 40) {
                for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                    if(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition() == -1) {
                        if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                            chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                            found = true;
                        }
                    }
                }
                if(found == false) {
                    for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                        if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                            chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                        }
                    }
                }
            }
            // HOME
            else {
                for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                    if(this.gameInfo.getPosibleMovesFields().get(i).getPosition() >=  this.gameInfo.getGameBoardSize()) {
                        if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                            chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                            found = true;
                        }
                    }
                }
                if(found == false) {
                    for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                        if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                            chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                        }
                    }
                }
            }
        }
        //  PREFER
        //  NEW
        else if(this.preferNew) {
            boolean found = false;
            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(0).getNumber()]);
            chosenMove = this.gameInfo.getPosibleMovesFields().get(0).getPosition();
            for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                if(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition() == -1) {
                    if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                        this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                        chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                    }
                }
            }
            if(found == false) {
                for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                    if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                        this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                        chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                    }
                }
            }
        }
        //  PREFER
        //  NOTHING
        else {
            boolean found = false;
            this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(0).getNumber()]);
            chosenMove = this.gameInfo.getPosibleMovesFields().get(0).getPosition();
            for(int i = 0; i < this.gameInfo.getPosibleMovesFigurines().size(); i++) {
                if(this.jumpSize(this.gameInfo.getSelectedFigurine().getActualPosition(), chosenMove) < this.jumpSize(this.gameInfo.getPosibleMovesFigurines().get(i).getActualPosition(), this.gameInfo.getPosibleMovesFields().get(i).getPosition())) {
                    this.gameInfo.setSelectedFigurine(this.figurines[this.gameInfo.getPosibleMovesFigurines().get(i).getNumber()]);
                    chosenMove = this.gameInfo.getPosibleMovesFields().get(i).getPosition();
                }
            }
        }
        System.out.print("Figurine n.");
        System.out.print(this.gameInfo.getSelectedFigurine().getNumber());
        System.out.print(" on position:");
        System.out.print(this.gameInfo.getSelectedFigurine().getActualPosition());
        System.out.println(" will move to field: " + chosenMove);
        System.out.println("CPU has performed it's move.");
        this.gameInfo.setPosibleMovesFields(null);
        this.gameInfo.setPosibleMovesFigurines(null);
        this.gameManagement.moveFigurine(this.gameInfo.getFields()[chosenMove]);
    }

    public int jumpSize(int act_pos, int new_pos) {
        if(act_pos == -1) return 1;
        if(act_pos >= new_pos) {
            return act_pos - new_pos;
        }
        else {
            if(new_pos >= this.gameInfo.getGameBoardSize()) {
                return ((this.gameInfo.getGameBoardSize()-1)-act_pos)+(new_pos-this.gameInfo.getGameBoardSize()-1);
            }
            else {
                return new_pos - act_pos;
            }
        }
    }

    public void setRandomMove(boolean flag) {
        this.randomMove = flag;
    }

    public boolean getRandomMove() {
        return this.randomMove;
    }

    public void setPreferHome(boolean flag) {
        this.preferHome = flag;
    }

    public boolean getPreferHome() {
        return this.preferHome;
    }

    public void setPreferNew(boolean flag) {
        this.preferNew = flag;
    }

    public boolean getPreferNew() {
        return this.preferNew;
    }
}
